package com.mku.parkingmanagement_service.vo.comm;

import com.mku.parkingmanagement_service.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @className: ResultVO
 * @author: Maoxx
 * @date: 2024/11/7 22:50
 * @Version: 1.0
 * @description:
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultVO <T> {
    private Boolean flag;
    private String code;
    private String message;
    private T data;


    public ResultVO(Boolean flag, String code, String message) {
        this.flag = flag;
        this.code = code;
        this.message = message;
    }

    /**
     * 成功，并返回数据
     * @param data 数据
     */
    public static <T> ResultVO<T> success(T data) {
        return new ResultVO<>(
                ResultEnum.SUCCESS.getState(),
                ResultEnum.SUCCESS.getCode(),
                ResultEnum.SUCCESS.getMessage(),
                data
        );
    }

    /**
     * 成功，但不返回数据
     */
    public static <T> ResultVO<T> success() {
        return new ResultVO<>(
                ResultEnum.SUCCESS.getState(),
                ResultEnum.SUCCESS.getCode(),
                ResultEnum.SUCCESS.getMessage()
        );
    }

    /**
     * 失败，并返回错误信息
     * @return
     * @param <T>
     */
    public static <T> ResultVO<T> fail() {
        return new ResultVO<>(
                ResultEnum.ERROR.getState(),
                ResultEnum.ERROR.getCode(),
                ResultEnum.ERROR.getMessage()
        );
    }

    /**
     * 失败，并返回自定义错误信息喝数据
     * @param message 错误信息
     * @param data 数据
     */
    public static <T> ResultVO<T> fail(String message,T data) {
        return new ResultVO<>(
                ResultEnum.FAIL.getState(),
                ResultEnum.FAIL.getCode(),
                message,
                data
        );
    }


    /**
     * 失败，并返回自定义数据
     * @param data 数据
     */
    public static <T> ResultVO<T> fail(T data) {
        return new ResultVO<>(
                ResultEnum.FAIL.getState(),
                ResultEnum.FAIL.getCode(),
                ResultEnum.FAIL.getMessage(),
                data
        );
    }

    /**
     * 自定义规范
     */
    public ResultVO<T> flag(Boolean flag) {
        this.flag = flag;
        return this;
    }

    public ResultVO<T> code(String code) {
        this.code = code;
        return this;
    }

    public ResultVO<T> message(String message) {
        this.message = message;
        return this;
    }

    public ResultVO<T> data(T data) {
        this.data = data;
        return this;
    }

}
